<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客列表</title>
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet" href="css/blog_list.css">
    <script src="js/jquery.min.js"></script>
    <!-- 别忘记引入依赖!!!!!!!!杨已萱你真蠢 -->
    <script src="js/logout.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo2.jpg" alt="">
        <span class="title">我的博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="blog_list.html">主页</a>
        <a href="blog_add.html">写博客</a>
        <a href="javascript:logout()">注销</a>
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 左侧个人信息 -->
        <!-- <div class="container-left">
            <div class="card">
                <img src="img/avatar.png" class="avtar" alt="">
                <h3>小可爱</h3>
                <a href="http:www.github.com">github 地址</a>
                <div class="counter">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <div class="counter">
                    <span>2</span>
                    <span>1</span>
                </div>
            </div>
        </div> -->
        <!-- 右侧内容详情 -->
        <div id="artListDiv" class="container-right">
            <!-- 每一篇博客包含标题, 摘要, 时间 -->
            
        </div>
    </div>
    <script>
        function init(){
            jQuery.ajax({
                url:"/art/mylist",
                type:"GET",
                data:{},
                success:function(res){
                    if(res.code == 200){
                        //请求成功
                        var createHTML ="";
                        var artList = res.data;
                        if(artList == null || artList.length == 0){
                            //没有文章
                            createHTML += "<h3 style = 'margintop=20px;marginleft=20px'> 还未添加文章,"+
                                "<a href='blog_add.html'>点此添加</a></h3>";
                        }else{
                            //有文章
                            for(var i = 0;i < artList.length;i++){
                                var art = artList[i];
                                //将每篇文章的内脏归位
                                createHTML += '<div class="blog" >';
                                createHTML += '<div class="title">'+ art.title +'</div>';
                                createHTML += '<div class="date">'+ art.createtime +'</div>';  
                                // createHtml += '<div class="date">'+art.createtime+'</div>';
                                createHTML += '<div class="desc">';
                                    //此处是列表页需要显示简介,后端并发处理截取
                                createHTML +=  art.content;
                                createHTML += '</div>';

                                createHTML += '<a href="blog_content.html?aid='+
                                art.id+'" class="detail">查看全文 &gt;&gt;</a>&nbsp;&nbsp;';

                                createHTML += '<a href="blog_edit.html?aid='+
                                art.id+'" class="detail">修改 &gt;&gt;</a>&nbsp;&nbsp;';

                                createHTML += ' <a href="javascript:del('+art.id+')" class="detail">删除 &gt;&gt;</a>';
                               
                                createHTML += '</div>';    
                            }

                        }
                        //innerHTML
                        jQuery("#artListDiv").html(createHTML);

                    }else{
                        alert("抱歉操作失败!"+res.msg);
                    }
                }
            })

        }
        init();
        //删除操作:
        function del(aid){
            //1.参数校验,防止恶意调用
            if(aid == ""||aid <=0){
                alert("非法参数!");
                return false;
            }
            //2.给后端,让后端删除
            jQuery.ajax({
                url:"/art/del",
                type:"POST",
                data:{
                    "aid":aid
                },
                success:function(res){
                    if(res.code == 200 && res.data == 1){
                        //删除成功,刷新页面
                        alert("删除成功!");
                        location.href = location.href;
                    }else{
                        alert("删除失败!!"+res.msg);
                    }
                }

            });

            //3.将结果展示给用户
        }
    </script>
</body>

</html>